﻿using BaseData.API.Read.Applications.Commands;
using BaseData.API.Read.Dtos;
using BaseData.Domain;
using BaseData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.API.Read.Controllers
{
    /// <summary>
    /// API控制器--读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class BaseDataController : ControllerBase
    {
        private readonly ILogger<BaseDataController> logger;
        private readonly IMediator mediator;

        public BaseDataController(ILogger<BaseDataController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public Task<ApiResult<UserDTO>> Login(LoginCommand request)
        {
            try
            {
                logger.LogInformation("-登录操作日志-");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 管理用户配置角色显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        
        public Task<ApiResult<ApiPaging<List<UsersDTO>>>> UserQuery([FromQuery]UserQueryCommand request)
        {
            try
            {
                logger.LogInformation("-管理用户显示操作日志-");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 管理用户配置角色配置功能显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<List<UserByMenuDTO>>>> UserByMenu([FromQuery]UserByMenuQueryCommand request)
        {
            try
            {
                logger.LogInformation("-管理用户配置角色配置功能显示操作日志-");
                return mediator.Send(request);

            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 权限管理查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<List<PermissionDTO>>>> PermissionQuery([FromQuery]PermissionQueryCommand request)
        {
            try
            {
                logger.LogInformation("-权限管理查询操作日志-");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 角色管理查询
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回查询任务</returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<List<RoleDTO>>>> RoleQuery([FromQuery]RoleQueryCommand request)
        {
            try
            {
                logger.LogInformation("-角色管理查询操作日志-");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 绑定角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<Role>>> BindRole()
        {
            try
            {
                logger.LogInformation("-绑定角色操作日志-");
                return mediator.Send(new BindRoleCommand());
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 绑定权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<Permission>>> BindPermission()
        {
            try
            {
                logger.LogInformation("-绑定权限操作日志-");
                return mediator.Send(new BindPermissionCommand());
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 绑定功能权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<MenuPermission>>> BindMenuPer()
        {
            try
            {
                logger.LogInformation("-绑定功能权限操作日志-");
                return mediator.Send(new BindMenuPerCommand());
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
    }
}
